package Gui.Dialogs;

import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;

import Cards.Card;
import Cards.EventCardLoss;
import Gui.GameFrame;
import Main.Defines;

public class EventDayDialog extends JDialog {
	/**
	 * 
	 */
	private static final long serialVersionUID = 8037971907373892360L;
	private JLabel TitleLabel = null;
	private JLabel ValueLabel = null;
	private JButton buttonOK = null;
	private Integer value = 0;
	private Integer cost = 0;
	private JPanel jp;
	private void buildUIContents(final Card theEvent){
		TitleLabel = new JLabel(theEvent.getName());

		value = theEvent.getValue();

		if (theEvent.getM_cardType() == Card.EventCardLoss) {
			cost = ((EventCardLoss) theEvent).getCost();
			ValueLabel = new JLabel("You pay $" + cost.toString());
		} else {
			ValueLabel = new JLabel("You receive $" + value.toString());
		}

		buttonOK = new JButton("Ok");
		buttonOK.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				Defines.CP().AddCash(value-cost);
				while ( Defines.CP().getCash() < 0 ){
					Defines.CP().AddCash(Defines.LOAN_AMOUNT);
					Defines.CP().AddLoan(Defines.LOAN_AMOUNT);
				}
				dispose();
			}
		});
		jp.add(TitleLabel);
		jp.add(ValueLabel);
		jp.add(buttonOK);
	}
	public EventDayDialog(final Card theEvent) {
		setModalityType(getModalityType().APPLICATION_MODAL);
		jp = new JPanel();
		jp.setBorder(Defines.getEmptyBorderConstraints());
		jp.setLayout(new GridLayout(0, Defines.SINGLE_ROW_COLUMN_MODIFIER));
		
		buildUIContents(theEvent);
		add(jp);
		pack();
		
		if(getTitle() != null){
			FontMetrics fm = getFontMetrics(getFont());
			int width = fm.stringWidth(getTitle()) + Defines.ICON_WIDTH;
			width = (int) Math.max(width*1.2, getPreferredSize().getWidth());
			setSize(new Dimension(width, getHeight()));
		}
		setLocationRelativeTo(GameFrame.getInstance());
		setVisible(true);
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
	}
}